<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 文件轨迹
 * @author xuelange
 * @editor Calvin
 *
 */
class Trace extends MY_Controller{
	private $rows = 100;
	public function __construct(){
		parent::__construct();
		$this->load->service('user_files_service');
		$this->load->service('file_trace_service');
	}
	
	public function index($active=""){
		$this->head_user_info();
		$files=null;
		$dates=null;
		$traces=null;
		$id = 0;
		$files_data = $this->file_trace_service->get_trace_files($this->uid,array("deleted"=>0),"ts","desc","id,name",array(0,$this->rows),$active);
		if(!empty($files_data))
		{
			$files = $this->file_format($files_data['rows']);
			$id = $files[0]['id'];
			$dates_data = $this->file_trace_service->get_dates($id,"ts","desc");
			if(!empty($dates_data)){
				$dates=$dates_data['rows'];
				$dates  = $this->date_format($dates);
				$traces_data = $this->file_trace_service->get_traces($id,array("hidden"=>0,"date"=>$dates[0]['ts']),"ts","desc","id,actor
,event,start,end,relation,ts,unsafe,hidden,file_id,actor_ip,actor_city");
				if(!empty($traces_data)){
					$traces=$this->file_trace_service->traces_format($traces_data['rows'],0);
				}
			}
		}
		$data = array(
				'uuid'		 => $id,
				'files' 	 => $files,
				'dates'  	 => $dates,
				'traces' => $traces,
				"fileTotal" => count($files),
				"dateTotal" => count($dates),
				"traceTotal"=> count($traces),
				"s_id"	=> $this->sid,
				"rows"	=> $this->rows,
				'file_empty' => empty($files)?1:0,
				'error_info' => '暂时还没有文件轨迹 '
		);
		$this->template->view('trace.tpl',$data);
	}
	
	
	private function array_unique_fb($array2D){
		foreach ($array2D as $k=>$v)
	    {
	        $v = join(",",$v);  //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
	        $temp[$k] = $v;
	    }
	    $temp = array_unique($temp);    //去掉重复的字符串,也就是重复的一维数组
		$i = 0;
	    foreach ($temp as $k => $v)
	    {
	        $array=explode(",",$v);     //再将拆开的数组重新组装
	        if($k == 0){
	        	$temp2[$i]['active'] = "active";
	        }
	        if(($k+1) % 2 == 0){
	        	$temp2[$i]['col2'] = "col2";
	        }
	        $temp2[$i]["ts"] =$array[0]; 
	        $temp2[$i]['index'] = $i;
	        $i++;
	    }
	    return $temp2;
	}

	private function date_format($dates,$start=0,$active=0)
	{
		$data = array();
		$temp = array();
		foreach($dates as $k=>$v)
		{
			$v['ts'] = date("Y-m-d",$v['ts']);
			if(in_array($v['ts'],$temp))
			{
				continue;
			}
			if($k == $active){
				$data[$k]['active'] = "active";
			}
			if(($k+1) % 2 == 0){
				$data[$k]['col2'] = "col2";
			}
			$data[$k]['index'] = $start+$k;
			$data[$k]['ts'] = $v['ts'];
			$temp[]=$v['ts'];
		}
		return array_values($data);
	}

	/**
	 * 获取更多的文件
	 *
	 * @get $file_id
	 * @get $start
	 *
	 */

	public function get_more_file()
	{
		$start = $this->input->get("start",true);
		$files_data = $this->file_trace_service->get_trace_files($this->uid,array("deleted"=>0),"ts","desc","id,name",array($start,$this->rows));
		$files = $files_data?$this->file_format($files_data['rows'],$start):null;
		Common::prf_json(true,$files);
	}


	/**
	 *
	 * @param bool $my
	 */
	public function get_by_file_id(){
		//sleep(3);
		$file_id = $this->input->get("file_id",TRUE);
		$my = $this->input->get("my",TRUE);

		if(empty($file_id)){
			common::prf_json ( TRUE, NULL, NULL,time(),TRUE, "file_id is null");
			return;
		}

		$dates_data = $this->file_trace_service->get_dates($file_id,"ts","desc");
		if(!$dates_data){
			common::prf_json ( TRUE, NULL, NULL,time(),TRUE, "dates list null");
			return;
		}
		$dates = $dates_data['rows'];
		/*foreach($dates as $key => $date){
			$dates[$key]['ts'] = date('Y-m-d', $date['ts']);
		}*/
		//var_dump($dates);exit;
		$dates = $this->date_format($dates);
		$condition = array("hidden"=>0,"date"=>$dates[0]['ts']);
		if(!empty($my))
		{
			$condition['actor']=$this->uid;
		}
		$traces_data = $this->file_trace_service->get_traces($file_id,$condition,"ts","desc","id,actor
,event,start,end,relation,ts,unsafe,hidden,file_id,actor_ip,actor_city");
		/*if(!$traces_data){
			common::prf_json ( TRUE, NULL, NULL,time(),TRUE, "trace list null");
			return;
		}*/
		$traces = $this->file_trace_service->traces_format($traces_data['rows'],0);
		/*if(!$traces){
			common::prf_json ( TRUE, NULL, NULL,time(),TRUE, "trace list null");
			return;
		}*/

		$data = array(
				'dates'  	 => $dates,
				'traces' => $traces,
				'dateTotal' => $dates_data['total'],
				"traceTotal" => $traces_data['total']
		);
		common::prf_json ( TRUE, $data, NULL,time(),TRUE);
		
	}


	public function get_by_date()
	{
		$date = $this->input->get("date");
		$file_id = $this->input->get("file_id");
		$my = $this->input->get("my",TRUE);
		if(empty($file_id) || empty($date))
		{
			show_404();
		}
		$condition = array("hidden"=>0,"date"=>$date);
		if(!empty($my))
		{
			$condition['actor']=$this->uid;
		}
		$traces_data = $this->file_trace_service->get_traces($file_id,$condition,"ts","desc","id,actor
,event,start,end,relation,ts,unsafe,hidden,file_id,actor_ip,actor_city");
		if(!$traces_data){
			common::prf_json ( TRUE, NULL, NULL,time(),TRUE, "trace list null");
			return;
		}
		$traces=$this->file_trace_service->traces_format($traces_data['rows'],0);
		common::prf_json ( TRUE, $traces, NULL,time(),TRUE);
	}

	
	public function fromUuid(){
		header("Content-Type: text/html; charset=UTF-8");
		$uuid = $this->input->get("file_id");
		if(strlen($uuid) == 0){
			self::index();
			return;
		}
		$this->db->select("uuid");
		$this->db->where("deleted","0");
		$this->db->order_by ( "creation_ts", "desc" );
		$this->db->group_by("uuid");
		$this->db->join("trace","trace.file_uuid = file.uuid");
		$query= $this->db->get_where("file",array("uid" => $this->uid));
		$fileList = $query->result_array();
		$query->free_result();
		
		$begin = 0;
		foreach ($fileList as $k => $f){
			if($f['uuid']== $uuid){
				$begin = $k;
				break;
			}
		}
		
		$files = $this->trace_model->getFiles($this->uid,$begin);
		
		$len = count($files);
		if($len < $this->fPage){
			$fileTotal = $this->trace_model->getFileTotal($this->uid);//总行数;
			$begin = $fileTotal - $this->fPage;
			if($begin<0){
				$begin = 0;
			}
			$files = $this->trace_model->getFiles($this->uid,$begin);
		}
		
		$files = self::fileFomat($files,$begin);
		
		if(!$files){
			$data = array(
					'file_empty' => 1,
					'error_info' => '暂时还没有文件轨迹 ',
					"s_id"	=> $this->sid
			);
			$this->template->view('trace.tpl', $data);
			return;
		}
		foreach ( $files as $k => $file ) {
			$files[$k]['ext_name'] = Common::get_file_type ( $file ['name'] );
		}
		
		$dates = $this->trace_model->getDates($uuid);
		if(!$dates){
			$data = array(
					'file_empty' => 1,
					'error_info' => '暂时还没有文件轨迹 ',
					"s_id"	=> $this->sid
			);
			$this->template->view('trace.tpl', $data);
			return;
		}
		foreach($dates as $key => $date){
			$dates[$key]['ts'] = date('Y-m-d', $date['ts']/1000);
		}
		$dates = self::array_unique_fb($dates);
		
		
		$traces = $this->trace_model->get_traces($uuid);
		if(!$traces){
			$data = array(
					'file_empty' => 1,
					'error_info' => '暂时还没有文件轨迹 ',
					"s_id"	=> $this->sid
			);
			$this->template->view('trace.tpl', $data);
		}
		
		
		
		$fileTotal = $this->trace_model->getFileTotal($this->uid);
		
		$dateTotal = self::dateTotal($uuid);//总行数;
		
		$traceTotal = $this->trace_model->getTraceTotal($uuid);
		$data = array(
				'uuid'		=> $uuid,
				'files'  	=> $files,
				'dates'  	=> $dates,
				'traces' 	=> $traces,
				"fileTotal" => $fileTotal,
				"dateTotal" => $dateTotal,
				"traceTotal"=> $traceTotal,
				"s_id"		=> $this->sid
		);
		$this->template->view('trace.tpl',$data);
	}
	
	private function file_format($files,$begin = 0){
		foreach ($files as $key => $file){
			$files[$key]['index'] = $begin + $key;
			$files[$key]['uuid'] = $file['id'];

		}
		return $files;
	}
	
}
